package algorithm.view.sort.insert;

import algorithm.view.common.AlgoHelpler;
import algorithm.view.prepare.Circle;

import java.awt.*;

/**
 * @author bingo
 * @Description
 * @Date 2018/4/29
 */
public class AlgoVisualizer {

    private AlgoFrame algoFrame;

    private  InsertSortData insertSortData;

    private  static  final  int  T = 20;


    public AlgoVisualizer(int senceWeight,int senceHeight,int N,int R) {
        EventQueue.invokeLater(()-> {
           algoFrame = new AlgoFrame("插入排序可视化", senceWeight, senceHeight);
            insertSortData =  new InsertSortData(N,R);
            AlgoHelpler.pause(1000);
            new Thread(()->{
                setData(0,-1,-1);
                for(int i=1;i<insertSortData.N();i++){
                    for(int j=i;j>0;j--){
                        setData(i,j,j-1);
                        if(insertSortData.get(j)<insertSortData.get(j-1)){
                            insertSortData.swap(j,j-1);
                            setData(i,-1,-1);
                        }
                        else{
                            break;
                        }
                    }
                }
                setData(insertSortData.N(),-1,-1);
            }).start();

        });
    }


    public void setData(int orderIndex,int currentIndex,int compareToIndex){
        insertSortData.orderedIndex = orderIndex;
        insertSortData.currentIndex = currentIndex;
        insertSortData.compareToIndex = compareToIndex;
        algoFrame.render(insertSortData);
        AlgoHelpler.pause(T);

    }
}
